﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeneratorLib.DB.Schemas
{
    [Serializable]
    public class TableInfoAddin
    {
        public string TableName { get; private set; }
        public string TableDisplayName { get; set; }
        public List<FieldInfoAddin> FieldAddins { get;private set; }
        public List<string> ListFields { get; private set; }
        public List<string> SearchFields { get; private set; }

        public TableInfoAddin(string  tableName)
        {
            this.TableName = tableName;
            TableDisplayName = "";
            FieldAddins = new List<FieldInfoAddin>();
            ListFields = new List<string>();
            SearchFields = new List<string>();
        }

        public void SetFieldAddins(string addinText)
        {
            FieldAddins.Clear();
            System.IO.StringReader sr = new System.IO.StringReader(addinText);
            while (sr.Peek()>0)
            {
                string line = sr.ReadLine();
                string[] fd = line.Split(new char[] { '=' });
                if (fd.Length >1)
                {
                    int index = line.IndexOf("=");
                    string f = line.Substring(0, index );
                    string d = line.Substring(index + 1);
                    FieldInfoAddin fia=FindFieldAddin(f);
                    if (fia == null)
                    {
                        FieldAddins.Add(new FieldInfoAddin
                        {
                            FieldName = f,
                            DisplayName = d
                        });
                    }
                    else
                    {
                        fia.FieldName = f;
                        fia.DisplayName = d;
                    }
                }
            }
        }

        public FieldInfoAddin FindFieldAddin(string fieldName)
        {
            return FindFieldAddin(fieldName, "");
        }

        public FieldInfoAddin FindFieldAddin(string fieldName,string displayName)
        {
            FieldInfoAddin f= FieldAddins.Find(m => m.FieldName.ToLower().CompareTo(fieldName.ToLower()) == 0);
            if (f == null)
            {
                f = new FieldInfoAddin();
                f.FieldName = fieldName;
                f.DisplayName = displayName;// fieldName;
                FieldAddins.Add(f);
            }
            return f;
        }

        public string GetFieldDisplayName(string fieldName)
        {
            FieldInfoAddin addin = FindFieldAddin(fieldName);
            if (addin != null)
                return addin.GetDisplay();
            else
                return "";
        }

        public bool IsListField(string field)
        {
            return ListFields.Contains(field);
        }

        public void AddListField(string field)
        {
            if (!ListFields.Contains(field))
            {
                ListFields.Add(field);
            }
        }

        public void RemoveListField(string field)
        {
            if (ListFields.Contains(field))
            {
                ListFields.Remove(field);
            }
        }

        public bool IsSearchField(string field)
        {
            return SearchFields.Contains(field);
        }

        public void AddSearchField(string field)
        {
            if (!SearchFields.Contains(field))
            {
                SearchFields.Add(field);
            }
        }

        public void RemoveSearchField(string field)
        {
            if (SearchFields.Contains(field))
            {
                SearchFields.Remove(field);
            }
        }


        public override int GetHashCode()
        {
            return this.TableName.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            TableInfoAddin addin= obj as TableInfoAddin;
            if (addin==null) return false;
            return this.TableName.Equals(addin.TableName);
        }

    }

    [Serializable]
    public class FieldInfoAddin
    {
        public string FieldName { get; set; }
        public string DisplayName { get; set; }

        public  string GetDisplay()
        {
            string pattern = @".*(?=[\(（])";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);
            if (string.IsNullOrEmpty(DisplayName))
            {
                return FieldName;
            }

            System.Text.RegularExpressions.Match match = regex.Match(DisplayName);
            if (match.Success)
                return match.Value.Trim();
            else
            {
                if (DisplayName.Trim().Length > 0)
                    return DisplayName.Trim();
                else
                    return FieldName;
            }
        }

    }

}
